package com.sbl.bajw.pc.service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.sbl.bajw.common.DataTable;
import com.sbl.bajw.common.DbManager;
import com.sbl.bajw.mapper.PatrolDao;
import com.sbl.bajw.pc.model.PatrolModel;
import com.sbl.bajw.pc.model.UserModel;

@Service
public class PatrolService {

	@Resource
	private PatrolDao dao;
	
	@Resource
	private UserService service;
	
	SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
	SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	public void insert(PatrolModel model) throws Exception{
		dao.insert(model);
	}
	
	public void update(PatrolModel model) throws Exception{
		dao.update(model);
	}
	
	public List<HashMap<String, Object>> getListApp(String id){
		List<HashMap<String, Object>> list = dao.getListApp(id);
		return list;
	}
	
	public PatrolModel getModel(String id){
		PatrolModel model = dao.getModel(id);
		return model;
	}
	
	public PatrolModel getLastPatrolInfo(String id){
		PatrolModel model = dao.getLastPatrolInfo(id);
		HashMap<String, BigDecimal> map = dao.getUserPatrolTime(id);
		if(model != null){
			if(model.getTim() >= 0){
				model.setSumtime(map.get("SUMTIME").intValue());
			}else{
				model.setSumtime(map.get("SUMTIME").intValue() - model.getTimeLength());
			}
		}
		return model;
	}
	
	public String historyLocation(PatrolModel model) throws Exception{
		String json = null;
		try {
			DbManager.beginTransaction();
			json = getHistoryLocation(model);
			DbManager.commit();
		} catch (Exception e) {
			e.printStackTrace();
			DbManager.rollback();
			throw e;
		}
		return json;
	}
	
	public void updateUserLocation(String id, String timer) throws Exception{
		UserModel user = service.getModel(id);
		try {
			DbManager.beginTransaction();
			service.updateUsersLocationInJWT(user, timer);
			DbManager.commit();
		} catch (Exception e) {
			e.printStackTrace();
			DbManager.rollback();
			throw e;
		}
	}
	
	public String getHistoryLocation(PatrolModel model) throws Exception{
		
		Date date = sd.parse(model.getStartTime());
		long end = date.getTime() + (model.getTimeLength() + 1) * 60 * 1000;
		
		UserModel user = service.getModel(model.getCreater());
		
		String sql = "select p.lng,p.lat from dgjwt.dyngpsdata" + sdf.format(date) + "@ysdblink p" 
				+ " left join dgjwt.v_police_position@ysdblink d on p.EQUIPMENTCODE = d.EQUIPMENTCODE"
				+ " where d.no = ? and p.gpstime >= (to_timestamp(?,'yyyy-mm-dd hh24:mi:ss.ff9')+0)"
				+ " and p.gpstime >= to_date(?,'yyyy-mm-dd hh24:mi:ss')" 
				+ " order by gpstime asc";
		
		List<String> parm = new ArrayList<String>();
		parm.add(user.getUsername());
		parm.add(model.getStartTime());
		parm.add(sd.format(new Date(end)));
		DataTable table = DbManager.executeQuery(sql, parm);
		String json = table.toJson();
		return json;
	}
}
